package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HouseController
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-08-24 19:46
 * @Version 1.0
 */
@Controller
@RequestMapping("/house")
public class HouseController extends BaseController{
    @Reference
    private HouseService houseService;

    @Reference
    private DictService dictService;

    @Reference
    private CommunityService communityService;

    @Reference
    private HouseBrokerService houseBrokerService;

    @Reference
    private HouseImageService houseImageService;

    @Reference
    private HouseUserService houseUserService;

    @RequestMapping
    public String findPage(HttpServletRequest request, Model model){
        //接收查询条件和分页条件
        Map<String, Object> filters = getFilters(request);
        model.addAttribute("filters",filters);

        //2.查询指定指定的房源信息含分页信息
        PageInfo<House> page = houseService.findPage(filters);
        model.addAttribute("page",page);

        //3.查询所有的小区信息并存入model
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList",communityList);

        //4.查询6个字典项列表并存入model
        model.addAttribute("houseTypeList",dictService.findListByDictCode("houseType"));
        model.addAttribute("floorList",dictService.findListByDictCode("floor"));
        model.addAttribute("buildStructureList",dictService.findListByDictCode("buildStructure"));
        model.addAttribute("directionList",dictService.findListByDictCode("direction"));
        model.addAttribute("decorationList",dictService.findListByDictCode("decoration"));
        model.addAttribute("houseUseList",dictService.findListByDictCode("houseUse"));

        return "house/index";
    }

    @RequestMapping("/create")
    public String create(Model model){

        //3.查询所有的小区信息并存入model
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList",communityList);

        //4.查询6个字典项列表并存入model
        model.addAttribute("houseTypeList",dictService.findListByDictCode("houseType"));
        model.addAttribute("floorList",dictService.findListByDictCode("floor"));
        model.addAttribute("buildStructureList",dictService.findListByDictCode("buildStructure"));
        model.addAttribute("directionList",dictService.findListByDictCode("direction"));
        model.addAttribute("decorationList",dictService.findListByDictCode("decoration"));
        model.addAttribute("houseUseList",dictService.findListByDictCode("houseUse"));

        return "house/create";
    }

    @RequestMapping("/save")
    public String save(House house){
        houseService.insert(house);
        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id,Model model){
        House house = houseService.getById(id);
        model.addAttribute("house",house);

        //3.查询所有的小区信息并存入model
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList",communityList);

        //4.查询6个字典项列表并存入model
        model.addAttribute("houseTypeList",dictService.findListByDictCode("houseType"));
        model.addAttribute("floorList",dictService.findListByDictCode("floor"));
        model.addAttribute("buildStructureList",dictService.findListByDictCode("buildStructure"));
        model.addAttribute("directionList",dictService.findListByDictCode("direction"));
        model.addAttribute("decorationList",dictService.findListByDictCode("decoration"));
        model.addAttribute("houseUseList",dictService.findListByDictCode("houseUse"));
        return "house/edit";
    }

    @RequestMapping("/update")
    public String update(House house){
        houseService.update(house);
        return "common/successPage";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        houseService.delete(id);
        return "redirect:/house";
    }

    @RequestMapping("publish/{id}/{status}")
    public String publish(@PathVariable Long id,@PathVariable Integer status){
        houseService.publish(id,status);
        return "redirect:/house";
    }

    @RequestMapping("/{id}")
    public String show(Model model,@PathVariable Long id){
        House house = houseService.getById(id);
        model.addAttribute("house",house);
        Community community = communityService.getById(house.getCommunityId());
        model.addAttribute("community",community);
        List<HouseBroker> houseBrokerList =  houseBrokerService.findListByHouseId(id);
        model.addAttribute("houseBrokerList",houseBrokerList);
        List<HouseUser> houseUserList =  houseUserService.findListByHouseId(id);
        model.addAttribute("houseUserList",houseUserList);
        List<HouseImage> houseImage1List = houseImageService.findList(id,1);
        List<HouseImage> houseImage2List = houseImageService.findList(id,2);
        model.addAttribute("houseImage1List",houseImage1List);
        model.addAttribute("houseImage2List",houseImage2List);

        return "house/show";
    }
}
